package HuaweiOD;

import java.util.LinkedList;
import java.util.Scanner;

public class 表达式括号匹配 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        System.out.println(solve(str));
    }

    public static int solve(String str) {
        int ans = 0;

        LinkedList<Character> stack = new LinkedList<>();
        for (int i = 0; i < str.length(); i++) {
            Character c = str.charAt(i);

            if (c != '(' && c != ')') continue;

            if (stack.size() > 0 && c == ')') {
                if (stack.getLast() == '(') {
                    ans++;
                    stack.removeLast();
                    continue;
                }
                return -1;
            }

            stack.add(c);
        }
        if (stack.size() > 0) return -1;
        return ans;
    }
}
